package com.yaolaizaixian.merchant.home;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.utils.Toasty;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/15.
 * 权益扫一扫界面
 */
public class QunyiErweimaActivity extends BaseActivity {


    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.fl_my_container)
    FrameLayout flMyContainer;
    @BindView(R.id.ll_shoushu)
    AutoLinearLayout llShoushu;
    @BindView(R.id.ll_flashlight)
    AutoLinearLayout llFlashlight;
    @BindView(R.id.rl_bottom)
    AutoLinearLayout rlBottom;
    @BindView(R.id.activity_second)
    AutoRelativeLayout activitySecond;
    private boolean islight=true;

    @Override
    public void initView() {

        setContentView(R.layout.quanyisaoma);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initData() {

        tvTitleText.setText("扫描二维码");
        CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_quanyicamera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();

    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {

            Intent data=new Intent();
            data.putExtra("code", result);
            setResult(108, data);
            finish();

        }

        @Override
        public void onAnalyzeFailed() {

            Toasty.error(getApplicationContext(), "扫码失败", Toast.LENGTH_SHORT).show();

            finish();

            return;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_back, R.id.ll_shoushu, R.id.ll_flashlight})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ll_back:

                finish();

                break;

            case R.id.ll_shoushu:


                Intent intent=new Intent(this, InputQuanyiActivity.class);
                //获取订单二维码
                startActivityForResult(intent, 109);

                break;

            /*开关手电筒*/
            case R.id.ll_flashlight:

                if (islight) {

                   CodeUtils.isLightEnable(true);

                    islight=false;

                }
                else
                {
                    CodeUtils.isLightEnable(false);

                    islight=true;
                }

               break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK

            case 109:

                Bundle b=data.getExtras(); //data为B中回传的Intent
                String mcode=b.getString("code");//str即为回传的值

                Intent data1=new Intent();
                data.putExtra("code", mcode);
                setResult(108, data);
                finish();

                break;


            default:


                break;
        }
    }
}
